package cn.com.ghy.android.widget.task;

import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.ghy.android.bean.TaskBean;
import cn.com.ghy.android.exception.DisposeException;
import cn.com.ghy.android.service.NoteOpeService;
import cn.com.ghy.android.util.CommonStatic;
import cn.com.ghy.android.widget.ComStaticApp;
import cn.com.ghy.android.widget.R;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:最新任务
 * @author wuz  @date 2011-5-23 16:45:30
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class NewTask extends Activity {

	private ListView listView = null;
	private ProgressDialog progressDialog = null;

	private Intent intent = null;
	private Bundle bundle = null;

	private NewTaskListAdapter newTaskListAdapter = null;
	List<TaskBean> taskBeanList = null;

	private HttpThread httpThread = null;

	private DisposeException disposeException = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.new_task);
		getSession();
		findView();
		init();
		addListener();
	}

	@SuppressWarnings("unchecked")
	private void getSession() {
		disposeException = ((ComStaticApp) getApplication()).getDisposeException();
		taskBeanList = (List<TaskBean>) this.getIntent().getSerializableExtra("taskBeanList");
		if (taskBeanList.size() == 0) {
			Toast.makeText(NewTask.this, "暂时没有最新任务.", Toast.LENGTH_SHORT).show();
		}
	}

	private void findView() {
		listView = (ListView) this.findViewById(R.id.listNewTaskList);
	}

	private void init() {
		if (null != taskBeanList && taskBeanList.size() > 0) {
			newTaskListAdapter = new NewTaskListAdapter(NewTask.this, taskBeanList);
			listView.setAdapter(newTaskListAdapter);
		}
	}

	private void addListener() {
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				TaskBean bean = (TaskBean) parent.getItemAtPosition(position);
				httpThread = new HttpThread(handler);
				httpThread.doStart(bean, position);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
			case CommonStatic.INTENT_REQUEST_CODE :
				if (resultCode == RESULT_OK) {
					bundle = data.getExtras();
					int position = -1;
					position = bundle.getInt("position");
					if (position != -1) {
						newTaskListAdapter.remove(position);
					}
				}
				break;
		}

	}

	private class NewTaskListAdapter extends BaseAdapter {

		private Context context = null;
		private List<TaskBean> list = null;

		private NewTaskListAdapter(Context context, List<TaskBean> list) {
			this.context = context;
			this.list = list;
		}

		public int getCount() {
			return list.size();
		}

		public TaskBean getItem(int position) {
			return list.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			String inflater = Context.LAYOUT_INFLATER_SERVICE;
			LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(inflater);
			LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(
					R.layout.new_task_lay, null);
			TaskBean bean = list.get(position);
			TextView tv = (TextView) linearLayout.findViewById(R.id.labTxtTaskId);
			tv.setText(bean.getTaskId());

			tv = (TextView) linearLayout.findViewById(R.id.labTxtTopic);
			tv.setText(bean.getTitle());

			tv = (TextView) linearLayout.findViewById(R.id.labTxtDate);
			tv.setText(bean.getCdate());

			tv = (TextView) linearLayout.findViewById(R.id.labTxtStatus);
			tv.setText(bean.getState());
			return linearLayout;
		}

		public void remove(int position) {
			list.remove(position);
			this.notifyDataSetChanged();
		}
	}

	private class HttpThread extends Thread {
		private Handler handle = null;
		private TaskBean taskBean = null;
		private int position = -1;

		public HttpThread(Handler hander) {
			handle = hander;
		}

		public void doStart(TaskBean taskBean, int position) {
			this.taskBean = taskBean;
			this.position = position;
			progressDialog = new ProgressDialog(NewTask.this);
			progressDialog.setTitle("提示信息");
			progressDialog.setMessage("正在请求数据，请稍等......");
			progressDialog.setCancelable(false);
			progressDialog.show();
			this.start();
		}

		public void run() {
			Message message = null;
			try {
				List<TaskBean> list = null;
				NoteOpeService noteOpeService = new NoteOpeService();
				list = noteOpeService.GetTaskDetail(taskBean);
				progressDialog.dismiss();
				message = handle.obtainMessage();
				bundle = new Bundle();
				message.what = CommonStatic.STATE_NORMAL;
				bundle.putSerializable("taskBean", list.get(0));
				bundle.putInt("position", position);
				message.setData(bundle);
				handle.sendMessage(message);
			} catch (Exception ex) {
				progressDialog.dismiss();
				disposeException.handleException(ex);
			}
		}
	}

	private Handler handler = new Handler() {
		private int position = -1;

		public void handleMessage(Message m) {
			switch (m.what) {
				case CommonStatic.STATE_NORMAL :
					TaskBean taskBean = (TaskBean) m.getData().getSerializable("taskBean");
					position = m.getData().getInt("position");
					bundle = new Bundle();
					bundle.putSerializable("taskBean", taskBean);
					bundle.putInt("position", position);
					intent = new Intent(NewTask.this, TaskManage.class);
					intent.putExtras(bundle);
					NewTask.this.startActivityForResult(intent, CommonStatic.INTENT_REQUEST_CODE);
					break;
			}
		}
	};
}
